﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using ARR.UI.Views;
using ARR.UI.EnvoyImporterService;
using System.Collections.ObjectModel;
using ARR.UI.ArticlesService;
using System.Windows;
using ARR.UI.Commands;
using System.Windows.Input;
using ARR.Shared;
using ARR.UI.Models;
using System.ComponentModel;
using System.ServiceModel;

namespace ARR.UI.ViewModels
{
    public class ReportCategoryViewModel : WorkspaceViewModel
    {
        public IReportCategoryModel model;
        private ICommand validateCommand;
        private ICommand generateCommand;
        private bool isBusy = true;

        public ReportCategoryViewModel(IReportCategoryModel model)
        {
            base.DisplayName = Labels.ReportCategory;
            this.model = model;
            this.model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(model_PropertyChanged);
            this.IsBusy = false;

            CurrentStep = 0;
            HasPassedCurrentStep = null;
        }

        void model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Categories")
            {
                OnPropertyChanged<ReportCategoryViewModel>(p => p.Categories);
            }
        }

        public ICommand ValidateCommand
        {
            get
            {
                if (validateCommand == null)
                {
                    validateCommand = new RelayCommand(param => Validate());
                }
                return validateCommand;
            }
        }

        public ICommand GenerateCommand
        {
            get
            {
                if (generateCommand == null)
                {
                    generateCommand = new RelayCommand(param => Generate());
                }
                return generateCommand;
            }
        }

        public bool IsBusy 
        {
            get { return this.isBusy; }
            set { this.isBusy = value; }
        }

        public Category SelectedCategory { get; set; }

        public ObservableCollection<ImporterResult> Results
        {
            get
            {
                return new ObservableCollection<ImporterResult>(model.Results.OrderBy(x => x.Description));
            }
        }

        public ObservableCollection<Category> Categories
        {
            get
            {
                return new ObservableCollection<Category>(model.Categories.OrderBy(x => x.Code));
            }
        }

        public Visibility IsVisibleResultsGV
        {
            get
            {
                if (Results != null && Results.Any())
                {
                    return Visibility.Visible;
                }

                return Visibility.Collapsed;
            }
        }

        public int CurrentStep { get; set; }

        public bool? HasPassedCurrentStep { get; set; }

        private void Validate()
        {
            if (this.SelectedCategory != null)
            {
                IsBusy = true;
                OnPropertyChanged<ArticlesImporterViewModel>(p => p.IsBusy);

                ReportsService.ReportsServiceClient service = new ReportsService.ReportsServiceClient();
                service.ValidateCategoryReportGenerationCompleted += new EventHandler<ReportsService.ValidateCategoryReportGenerationCompletedEventArgs>(service_ValidateCategoryReportGenerationCompleted);
                service.ValidateCategoryReportGenerationAsync(WorkspaceData.Instance.RangeReview, this.SelectedCategory.Id);
            }
        }

        private void Generate()
        {
            if (this.SelectedCategory != null)
            {
                ReportsViewer viewer = new ReportsViewer(Views.ReportType.CategoryReport, WorkspaceData.Instance.RangeReview.Id, this.SelectedCategory.Id);
                viewer.Show();
            }
        }

        void service_ValidateCategoryReportGenerationCompleted(object sender, ReportsService.ValidateCategoryReportGenerationCompletedEventArgs e)
        {
            IsBusy = false;
            OnPropertyChanged<ArticlesImporterViewModel>(p => p.IsBusy);

            model.Results = e.Result;
            HasPassedCurrentStep = true;

            if (e.Error == null)
            {
                (sender as ICommunicationObject).Close();

                if (model.Results.Any())
                {
                    if (model.Results.Any(p => p.ErrorType == ErrorType.Error))
                    {
                        HasPassedCurrentStep = false;
                    }
                }
            }
            else
            {
                (sender as ICommunicationObject).Abort();
                throw e.Error;
            }

            OnPropertyChanged<ArticlesImporterViewModel>(p => p.HasPassedCurrentStep);
            OnPropertyChanged<ArticlesImporterViewModel>(p => p.Results);
            OnPropertyChanged<ArticlesImporterViewModel>(p => p.IsVisibleResultsGV);
        }
    }
}
